package com.lxt.controller;

import com.lxt.common.HttpResult;
import com.lxt.common.util.JsonUtils;
import com.lxt.common.util.RedisOperator;
import com.lxt.pojo.BO.ShopCartBO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Api(value = "购物车接口controller", tags = {"购物车接口相关的api"})
@RequestMapping("shopcart")
@RestController
public class ShopCartController extends BaseController {

    @Autowired
    private RedisOperator redisOperator;

    @ApiOperation(value = "添加商品到购物车", notes = "添加商品到购物车", httpMethod = "POST")
    @PostMapping("/add")
    public HttpResult add(@RequestParam String userId, @RequestBody ShopCartBO shopCartBO,
                          HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(userId)) {
            return HttpResult.errorMsg("");
        }

        // 前端用户在登录的情况下，添加商品到购物车，会同时在后端同步购物车到Redis
        // 需要额外判断当前购物车中包含已经存在的商品，如果存在则累计累加购买数量
        String shopCartJson = redisOperator.get(FOODIE_SHOP_CART + ":" + userId);
        List<ShopCartBO> shopCartList = null;
        //redis中已经有购物车数据了
        if (StringUtils.isNotBlank(shopCartJson)) {
            shopCartList = JsonUtils.jsonToList(shopCartJson, ShopCartBO.class);
            //判断购物车中是否存在已有商品，如果存在的话counts累加
            boolean isHaving = false;
            for (ShopCartBO sc : shopCartList) {
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(shopCartBO.getSpecId())) {
                    sc.setBuyCounts(sc.getBuyCounts() + shopCartBO.getBuyCounts());
                    isHaving = true;
                }
            }
            if (!isHaving) {
                shopCartList.add(shopCartBO);
            }
            //redis中没有购物车数据
        } else {
            shopCartList = new ArrayList<>();
            //直接添加到购物车中
            shopCartList.add(shopCartBO);
        }

        //覆盖现有redis中的购物车
        redisOperator.set(FOODIE_SHOP_CART + ":" + userId, JsonUtils.objectToJson(shopCartList));

        return HttpResult.ok();
    }

    @ApiOperation(value = "从购物车中删除商品", notes = "从购物车中删除商品", httpMethod = "POST")
    @PostMapping("/del")
    public HttpResult del(@RequestParam String userId, @RequestParam String itemSpecId,
                          HttpServletRequest request, HttpServletResponse response) {
        if (StringUtils.isBlank(userId) || StringUtils.isBlank(itemSpecId)) {
            return HttpResult.errorMsg("参数不能为空");
        }

        // 用户在页面删除购物车中的商品数据，如果用户此时已登录，则需要同步删除redis购物车中的商品
        String shopCartJson = redisOperator.get(FOODIE_SHOP_CART + ":" + userId);

        if (StringUtils.isNotBlank(shopCartJson)) {
            List<ShopCartBO> shopCartList = JsonUtils.jsonToList(shopCartJson, ShopCartBO.class);
            for (ShopCartBO sc : shopCartList) {
                String tmpSpecId = sc.getSpecId();
                if (tmpSpecId.equals(itemSpecId)) {
                    shopCartList.remove(sc);
                    break;
                }
            }
            //覆盖现有redis中的购物车
            redisOperator.set(FOODIE_SHOP_CART + ":" + userId, JsonUtils.objectToJson(shopCartList));
        }

        return HttpResult.ok();
    }
}
